package cn.xieyang.gen.database.mysql;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.xieyang.gen.database.mysql.impl.FixValGeneratorImpl;
import cn.xieyang.gen.database.mysql.impl.OrdCdeValGeneratorImpl;
import cn.xieyang.gen.database.mysql.impl.PkNoValGeneratorImpl;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.sql.SQLException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class GanjiangApp {

    public static void main(String[] args) throws SQLException, FileNotFoundException, ClassNotFoundException {
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        // 编号列
        func_00001();
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        // 日期列，当前日期往后666天
//        func_00002();
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        // 下划线列 → 驼峰列
//        func_00003();
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        // 计算耗时
        // 开始时间列和结束时间列 → 小时列、分钟列、秒列、毫秒列
//        func_00004();
        // 排序编号
//        func_00005();
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    }

    private static void func_00001() throws SQLException, FileNotFoundException, ClassNotFoundException {
        List<String> tblNameList = new ArrayList<>();
        tblNameList.add("interview_record_tbl");

        for (String tblName : tblNameList) {
            int rowsCount = 666;
            Map<String, ValGenerator> colMap = new LinkedHashMap<>();
            colMap.put("record_no", new PkNoValGeneratorImpl().setValGeneratorConfig(ValGeneratorConfig.builder().prefix("REC").build()));
            colMap.put("version_no", new FixValGeneratorImpl().setValGeneratorConfig(ValGeneratorConfig.builder().fixedValue("VER1967841271403569152").build()));
            colMap.put("ord_cde", new OrdCdeValGeneratorImpl().setValGeneratorConfig(ValGeneratorConfig.builder().build()));
            colMap.put("modify_version", new PkNoValGeneratorImpl().setValGeneratorConfig(ValGeneratorConfig.builder().prefix("MOD").build()));
            doGen(tblName, rowsCount, colMap);
        }
    }

    private static void doGen(String tblName, int rowsCount, Map<String, ValGenerator> colMap) throws SQLException, FileNotFoundException, ClassNotFoundException {
        StringBuilder sb1 = new StringBuilder();
        sb1.append("insert into `ganjiang-db`.`" + tblName + "` ");
        sb1.append("(");
        Iterator<String> colNameIterator = colMap.keySet().iterator();
        while (colNameIterator.hasNext()) {
            String colName = colNameIterator.next();
            sb1.append("`" + colName + "`");
            if (colNameIterator.hasNext()) sb1.append(",");
        }
        sb1.append(") values ");
        for (int i = 0; i < rowsCount; i++) {
            sb1.append("(");
            Iterator<ValGenerator> valIterator = colMap.values().iterator();
            while (valIterator.hasNext()) {
                ValGenerator valGenerator = valIterator.next();
                if (valGenerator.isNeedSingleQuotes()) {
                    sb1.append("'");
                }
                valGenerator.getValGeneratorConfig().setIndex(i);
                sb1.append(valGenerator.getVal());
                if (valGenerator.isNeedSingleQuotes()) {
                    sb1.append("'");
                }
                if(valIterator.hasNext()) sb1.append(",");
            }
            sb1.append("),");
        }
        sb1.deleteCharAt(sb1.length() - 1);
        sb1.append(";");
        writeInRsSqlFileAndImport(sb1);
    }

    private static void writeInRsFile(CharSequence sb1) {
        URL resource = ResourceUtil.getResource("rs/rs.txt");
        String file = resource.getFile();
        FileUtil.writeUtf8String(sb1.toString(), new File(file));
    }

    private static void writeInRsSqlFileAndImport(CharSequence sb1) throws SQLException, FileNotFoundException, ClassNotFoundException {
        URL resource = ResourceUtil.getResource("rs/rs.sql");
        String file = resource.getFile();
        FileUtil.writeUtf8String(sb1.toString(), new File(file));
        String path = resource.getPath();
        if (path.startsWith("/")) path = path.substring(1);
        ImportSql.doImport(path);
    }

    private static void func_00002() {
        LocalDate localDate = LocalDate.parse("2025-07-07").minusDays(1);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 666; i++) {
            localDate = localDate.plusDays(1);
            sb.append(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).append("\n");
        }
        ClipboardUtil.setStr(sb.toString());
        writeInRsFile(sb);
    }

    private static void func_00003() {
        String str = ClipboardUtil.getStr();
        StringBuilder sb = new StringBuilder();
        for (String s : Arrays.asList(str.split("\n"))) {
            sb.append(StrUtil.toCamelCase(s)).append("\n");
        }
        ClipboardUtil.setStr(sb.toString());
        writeInRsFile(sb);
    }

    private static void func_00004() {
        StringBuilder sb = new StringBuilder();
        String str = ClipboardUtil.getStr();
        String[] inputs = str.split("\n");

        // 创建日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

        for (String line : inputs) {
            // 拆分开始和结束时间
            String[] timestamps = line.split("\t");

            // 解析时间字符串
            LocalDateTime start = LocalDateTime.parse(timestamps[0], formatter);
            LocalDateTime end = LocalDateTime.parse(timestamps[1], formatter);

            // 计算时间间隔
            Duration duration = Duration.between(start, end);

            // 计算各个时间单位
            long days = duration.toDays();
            long hours = duration.toHours() % 24;
            long minutes = duration.toMinutes() % 60;
            long seconds = duration.getSeconds() % 60;
            long millis = duration.toMillis() % 1000;

            // 格式化和输出结果
            // 格式：天\t小时\t分\t秒\t毫秒\t\n
            sb.append(String.format("%d\t%d\t%d\t%d\t%d%n", days, hours, minutes, seconds, millis));
            // 格式：小时\t分\t秒\t毫秒\t\n
//            sb.append(String.format("%d\t%d\t%d\t%d%n", days, hours, minutes, seconds, millis));
        }
        ClipboardUtil.setStr(sb.toString());
        writeInRsFile(sb);
    }

    private static void func_00005() {
        StringBuilder sb = new StringBuilder();
        int count = 55;
        for (int i = 1; i <= count; i++) {
            sb.append(i).append("\n");
        }
        ClipboardUtil.setStr(sb.toString());
        writeInRsFile(sb);
    }

}
